package com.epam.training.service.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.training.domain.*;
import com.epam.training.repository.*;

@Service
public class UserServiceImpl implements UserService {
	private static final int PAGE_SIZE = 10;

	private final CustomerRepository customerRepository;
	private final AdminRepository adminRepository;

	@Autowired
	public UserServiceImpl(CustomerRepository customerRepository,
			AdminRepository adminRepository) {
		this.customerRepository = customerRepository;
		this.adminRepository = adminRepository;
	}

	@Override
	public List<Customer> getAllCustomers() {
		List<Customer> list = customerRepository.findAll();
		return list;
	}

	@Override
	public Page<Customer> getAllCustomers(Integer pageNumber) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, PAGE_SIZE);
		return customerRepository.findAll(pageRequest);
	}

	@Override
	public List<Admin> getAllAdmins() {
		List<Admin> list = adminRepository.findAll();
		return list;
	}

	@Override
	public Page<Admin> getAllAdmins(Integer pageNumber) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, PAGE_SIZE);
		return adminRepository.findAll(pageRequest);
	}

	@Transactional
	@Override
	public boolean save(Customer customer) {
		if (customerRepository.save(customer) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean save(Admin admin) {
		if (adminRepository.save(admin) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Customer findCustomerByLogin(String login) {
		return customerRepository.findByLogin(login);
	}

	@Override
	public void update(Customer customer, String login) {
		Customer user = customerRepository.findByLogin(login);
		customer.setCustomerId(user.getUserId());

		if (customer.getPassword().isEmpty()) {
			customer.setPassword(user.getPassword());
		}

		if (customer.equals(user)) {
			return;
		}

		user.setName(customer.getName());
		user.setSurname(customer.getSurname());
		user.setSecondName(customer.getSecondName());
		user.setDriveLicense(customer.getDriveLicense());
		user.setEmail(customer.getEmail());
		user.setTelephone(customer.getTelephone());
		user.setPassword(customer.getPassword());

		customerRepository.save(user);
	}

	@Override
	public Admin getAdminById(Integer id) {
		return adminRepository.findOne(id);
	}

	@Override
	public Customer getCustomerById(Integer id) {
		return customerRepository.findOne(id);
	}

	@Override
	public Admin findAdminByLogin(String login) {
		return adminRepository.findByLogin(login);
	}

	@Override
	public void update(Admin admin) {
		Admin adminOld = adminRepository.findByLogin(admin.getLogin());

		if (admin.getPassword().isEmpty()) {
			admin.setPassword(adminOld.getPassword());
		}

		if (admin.equals(adminOld)) {
			return;
		}

		adminOld.setName(admin.getName());
		adminOld.setPassword(admin.getPassword());
		adminOld.setLastAcces(new Date());

		adminRepository.save(adminOld);
	}

	@Override
	public void deleteCustomer(Integer id) {
		customerRepository.delete(id);
	}

	@Override
	public void deleteAdmin(Integer id) {
		adminRepository.delete(id);
	}
}
